package com.tuni.birthdaymoney.web.controller;

import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.spec.InvalidKeySpecException;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tuni.birthdaymoney.persistence.entity.User;
import com.tuni.birthdaymoney.service.UserService;
import com.tuni.birthdaymoney.service.util.security.HashProvider;
import com.tuni.birthdaymoney.web.model.MasterPage;
import com.tuni.birthdaymoney.web.model.ProfileSettingsPage;

@Controller
public class ProfileSettingsController {

    @Autowired
    private UserService userService;
    @Autowired
    private HashProvider hashProvider;

    @RequestMapping(value = "/settings/profile", method = RequestMethod.GET)
    public String profile_settings(Principal principal, Map<String, Object> settingsPageMap) {

        // create page models
        MasterPage masterPageModel = new MasterPage(userService.getUserByEmail(principal.getName()), false);
        ProfileSettingsPage profileSettingsPageModel = new ProfileSettingsPage();

        // save binding
        settingsPageMap.put("masterPageModel", masterPageModel);
        settingsPageMap.put("profileSettingsPageModel", profileSettingsPageModel);

        return "profile_settings";
    }

    @RequestMapping(value = "/settings/profile/save", method = RequestMethod.POST)
    public String profile_settings_save(Principal principal, @ModelAttribute("profileSettingsPageModel") ProfileSettingsPage settings,
            BindingResult result, Map<String, Object> settingsPageMap) {

        if (!result.hasErrors()) {

            try {

                User user = userService.getUserByEmail(principal.getName());

                if (user == null) {
                    // TODO: handle error
                    return "redirect:/home.html";
                }

                if (!hashProvider.validatePassword(settings.getOldPassword(), user.getHashPass(), user.getSalt())) {
                    // TODO: handle error
                    return "redirect:/home.html";
                }

                if (settings.getNewPassword().equals("")) {
                    // TODO: handle error
                    return "redirect:/home.html";
                }

                if (!settings.getNewPassword().equals(settings.getConfirmedPassword())) {
                    // TODO: handle error
                    return "redirect:/home.html";
                }

                Entry<String, String> pair = hashProvider.createHash(settings.getNewPassword());
                user.setSalt(pair.getKey());
                user.setHashPass(pair.getValue());

                userService.save(user);

            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvalidKeySpecException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // settings.getOldPassword()

            // settingsService.saveNotificationSettings(settings.getSettings());
        }

        return "redirect:/home.html";

        // settingsPageMap.put("notificationSettingsPageModel", settings);
        // return "notification_settings";
    }
}
